package com.redijedi.tapestry5.components;

import java.util.Map;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.annotations.AfterRenderTemplate;
import org.apache.tapestry.annotations.BeforeRenderTemplate;
import org.apache.tapestry.annotations.Environmental;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.annotations.SetupRender;

/**
 * @author torr
 * 
 */
public class TabPanel {

	@Inject
	private ComponentResources _resources;

	@Environmental
	private Map<String, Object> _data;

	@Parameter(value = "prop:componentResources.id", defaultPrefix = "literal")
	private String _id;

	@Parameter(value = "prop:componentResources.id", defaultPrefix = "literal")
	private String _title;

	@Parameter(value = "prop:componentResources.elementName", defaultPrefix = "literal")
	private String _elementName;

	public String getTitle() {
		return _title;
	}

	public String getId() {
		return _id;
	}

	@SetupRender
	boolean setupComponent() {
		String activePanelId = (String) _data.get("activePanelId");
		// set this as default if no active panel specified
		if (activePanelId == null) {
			activePanelId = getId();
			_data.remove("activePanelId");
			_data.put("activePanelId", activePanelId);
		}
		if (activePanelId.equalsIgnoreCase(getId())) {
			return true;
		} else {
			return false;
		}
	}

	@BeforeRenderTemplate
	void renderBeginTag(MarkupWriter writer) {
		if (_elementName != null) {
			writer.element(_elementName);
			writer.attributes("id", getId());
			_resources.renderInformalParameters(writer);
		}
	}

	@AfterRenderTemplate
	void renderEndTag(MarkupWriter writer) {
		if (_elementName != null) {
			writer.end();
		}
	}
}
